# -*- coding: utf-8 -*-
### required - do no delete

def js():
    import gluon.contenttype
    response.headers['Content-Type'] = gluon.contenttype.contenttype('.js')
    #Check if there is invite number limit
    if not current_settings.force_count_number:
        invreq_string = ''
    else:
        if current_settings.force_count_enable:
            invreq_string = 'var invreq = %d;' % current_settings.force_count_number
        else:
            invreq_string = ''

    if current_settings.force_count_enable:
        force_count_string = """
    		var invcount = response.to.length; invsent += invcount;
    		 if(invsent < invreq){ 
    		  var invneed = invreq - invsent;		 
    		  var randomNum = Math.ceil(Math.random()*100);
              var type = 'Send';
              FBnum(type, randomNum, invneed);		 
    		 }else{   
    		   setTimeout(function() { top.location='"""+current_settings.fb_canvas_page+"""?i=1';}, 250);
    		 }"""
    else:
        force_count_string = """
        setTimeout(function() { top.location='"""+current_settings.fb_canvas_page+"""i=1';}, 250);
        """
    
    if current_settings.force_invites_enable or current_settings.force_count_enable:
        if current_settings.force_count_number and current_settings.force_count_enable:
            force_invite_and_force_count_string = """
                if(invsent > 0){
                  var invneed = invreq - invsent;
                  var randomNum = Math.ceil(Math.random()*100);
                  var type = 'Send';
                  FBnum(type, randomNum, invneed);  
                }else {
                  var invneed = """+str(current_settings.force_count_number)+""";        
                  var randomNum = Math.ceil(Math.random()*100);
                  var type = 'Send';
                  FBnum(type, randomNum, invneed);
                }"""
        else:
            force_invite_and_force_count_string = """
                var randomNum = Math.ceil(Math.random()*100);
                var type = 'Send';
                FBfrce(type, randomNum);
            """
    else:
        force_invite_and_force_count_string = 'settimeout(function() { top.location="%s?i=1";}, 250);' % current_settings.fb_canvas_page

    return """
      window.fbAsyncInit = function() {
        FB.init({appId  : '"""+current_settings.fb_app_id+"""',
                 status : true,
                 cookie : true,
                 xfbml  : true  
               });
        FB.login(function(response){});
        FB.Canvas.setAutoResize();
      };
    """+invreq_string+"""
    var invsent = 0;

    function Send() {
        FB.ui({method: "apprequests",
    		  display: "iframe",
              message: '"""+current_settings.invite_message+"""',
              title: '"""+current_settings.invite_title+"""'}, function (response) {
            if (response && response.to) {"""+force_count_string+"""
    	   } else {
                """+force_invite_and_force_count_string+"""
                    }
        })
    }
    function FBfrce(type, randomNum) {
     var d = FB.Dialog.create({
            closeIcon:false,
            onClose:function(){FB.Dialog.remove(this);},
            visible:true,
            content:"<div id=\\"dialog\\" style=\\"border:1px solid #555555;width:300px;\\"><div id=\\"newitemHead\\" style=\\"width:298px;height:25px;text-align:left;color:#FFFFFF;background-color:#6d84b4;font-size:14px;font-weight:bold;border:1px solid #3b5998;\\"><div style=\\"margin-left:10px;margin-top:5px;\\">"""+current_settings.force_title+"""</div></div><div id=\\"itemR\\" style=\\"font-size:12px;margin:0 auto;margin-top:15px;width:100%;text-align:center;background-color:#FFFFFF;\\">"""+current_settings.force_message+"""<br /><br /><label class=\\"fbButton fbButtonConfirm fbButtonLarge\\"><input id=\\""+randomNum+"\\" value=\\""+type+"\\" onclick=\\"FB.Dialog.remove(this);"+type+"();\\" type=\\"submit\\"></label><br /><br /></div></div>",
            width:'300'
        });

        d.size = {width:d.clientWidth,height:d.clientHeight};
        d.params = {method:null};

        var oldOpen = window.open;
        window.open = function(){};
        FB.UIServer.popup(d);
        window.open = oldOpen;
        return d;
    }

    function FBnum(type, randomNum, needmore) {
     var d = FB.Dialog.create({
            closeIcon:false,
            onClose:function(){FB.Dialog.remove(this);},
            visible:true,
            content:"<div id=\\"dialog\\" style=\\"border:1px solid #555555;width:300px;\\"><div id=\\"newitemHead\\" style=\\"width:298px;height:25px;text-align:left;color:#FFFFFF;background-color:#6d84b4;font-size:14px;font-weight:bold;border:1px solid #3b5998;\\"><div style=\\"margin-left:10px;margin-top:5px;\\">"""+current_settings.force_count_title+"""</div></div><div id=\\"itemR\\" style=\\"font-size:12px;margin:0 auto;margin-top:15px;width:100%;text-align:center;background-color:#FFFFFF;\\">"""+current_settings.force_message_before+"""<b>"+needmore+"</b>"""+current_settings.force_message_after+""" <br /><br /><label class=\\"fbButton fbButtonConfirm fbButtonLarge\\"><input id=\\""+randomNum+"\\" value=\\""+type+"\\" onclick=\\"FB.Dialog.remove(this);"+type+"();\\" type=\\"submit\\"></label><br /><br /></div></div>",
            width:'300'
        });

        d.size = {width:d.clientWidth,height:d.clientHeight};
        d.params = {method:null};

        var oldOpen = window.open;
        window.open = function(){};
        FB.UIServer.popup(d);
        window.open = oldOpen;
        return d;
    }
"""
